#ifndef __COMM_HPP__
#define __COMM_HPP__

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include "log.hpp"  
using namespace std;

Log log;

const int size = 4096;

const string pathname = "/home/outlier";
const int proj_id = 0x6666;

key_t GetKey()
{
    key_t key = ftok(pathname.c_str(), proj_id);
    if(key == -1)
    {
        log(Fatal, "fotk error: %s", strerror(errno));
        exit(1);
    }
    //log(Info, "fotk success: 0x%x", key);
    return key;
}

int GetShareMemory(int shmflg)
{
    key_t key = GetKey();
    int shmid = shmget(key, size, shmflg);
    if(shmid == -1)
    {
        log(Fatal, "create share error: %s", strerror(errno));
        exit(2);
    }
    //log(Info, "create share success: %d", shmid);
    return shmid;
}

int CreateShm()
{
    return GetShareMemory(IPC_CREAT|IPC_EXCL|0666);
}

int Getshm()
{
    return GetShareMemory(IPC_CREAT);
}

#endif

#define FIFO_FILE "./myfifo"    
#define MODE 0664    
    
class Init    
{    
public:    
    Init()    
    {    
        int n = mkfifo(FIFO_FILE, MODE);    
        if(n == -1)     // 创建管道失败返回 -1    
        {    
            // perror("mkfifo");    
            log(Fatal, "create pipe done, error string: %s, error code: %d", strerror(errno), errno);    
            exit(1);    
        }    
    }    
    ~Init()    
    {    
        int m = unlink(FIFO_FILE);      // unlink 删除文件的系统调用    
        if(m == -1)    
        {    
            // perror("unlink");    
            log(Fatal, "unlink pipe done, error string: %s, error code: %d", strerror(errno), errno);    
            exit(2);    
        }
    }

private:
    Log log;
};
